import { defineStore } from 'pinia'
export interface CouponType {
    objectId: string,
    name: string,
    amount: number,
    integral: number,
    src: string,
    number: number,
    date: string,
}
export interface Couponstate {
    list: CouponType[]
}
export const useCoupon = defineStore('coupon', {
    state: (): Couponstate => ({
        list: [
            // {
            //     objectId: "",
            //     src: "",
            //     name: "",
            //     integral: 
            //     date: "",
            //     amount: "",
            //     number: ''
            // }
        ]
    }),
    actions: {
        couponAddAction(couponobj: CouponType) {
            // console.log(couponobj);

            // 替换新数组操作，list后面的数组对象整体发生改变才能触发响应式
            for (let i = 0; i < this.list.length; i++) {
                if (this.list[i].objectId === couponobj.objectId) {
                    this.list[i].number++;
                    localStorage.setItem("couponList", JSON.stringify(this.list))
                    return
                }
            }
            this.list = [...this.list, couponobj]
        },
        couponInitAction(List: CouponType[]) {
            // 初始化数据
            this.list = List
        }
    }
})